package com.cskaoyan.aspect;

import com.cskaoyan.bean.marketBean.MarketAdmin;
import com.cskaoyan.bean.marketBean.MarketLog;
import com.cskaoyan.mapper.marketMapper.MarketLogMapper;
import com.cskaoyan.mapper.marketMapper.MarketTopicMapper;
import com.cskaoyan.utils.ValidationUtil;
import com.cskaoyan.controller.PromotionController;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author stone
 * @date 2022/12/21 11:27
 */
@Component
@Aspect
public class CustomAspect {

    @Autowired
    PromotionController promotionController;
    @Autowired
    MarketLogMapper logMapper;

    @Pointcut("@annotation(com.cskaoyan.annotation.ExecutionLog)")
    public void methodPointcut() {
    }

    @Pointcut("@annotation(com.cskaoyan.annotation.ExecutionCreateLog)")
    public void methodPointcut2() {
    }

    @Around("methodPointcut()")
    public Object executeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            Class<?> aClass = arg.getClass();
            String typeName = aClass.getTypeName();
            if (typeName.equals("org.springframework.validation.BeanPropertyBindingResult")) {
                ValidationUtil.valid((BindingResult) arg);
            }
        }

        Object proceed = joinPoint.proceed();
        return proceed;
    }

    @Around("methodPointcut2()")
    public Object executeCreateLog(ProceedingJoinPoint joinPoint) throws Throwable {

        Object proceed = joinPoint.proceed();
        //需要加入日志的操作是  登录/退出/发货/增删改查管理员
        MarketLog marketLog = new MarketLog();

        //获取ip地址
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        String ip = request.getRemoteHost();
        marketLog.setIp(ip);

        MarketAdmin principal = (MarketAdmin) SecurityUtils.getSubject().getPrincipal();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        if (name.equals("login") && principal == null){
            marketLog.setAction("登录");
            marketLog.setType(0);
            marketLog.setAdmin("匿名用户");
            marketLog.setStatus(false);
        }
        //获取操作的管理员
        //需要加入日志的操作是  登录/退出/发货/增删改查管理员
        if (name.equals("login")){
            marketLog.setAction("登录");
        }else if (name.equals("logout")){
            marketLog.setAction("退出");
        }else if (name.equals("orderShip")){
            marketLog.setAction("发货");
        }else if (name.equals("adminCreate")){
            marketLog.setAction("新增管理员");
        }else if (name.equals("adminDelete")){
            marketLog.setAction("删除管理员");
        }else if (name.equals("adminUpdate")){
            marketLog.setAction("修改管理员");
        }

        marketLog.setType(1);
        String username = principal.getUsername();
        marketLog.setAdmin(username);
        marketLog.setAddTime(new Date());
        marketLog.setDeleted(false);
        marketLog.setStatus(true);
        marketLog.setUpdateTime(new Date());

        logMapper.insertSelective(marketLog);
        return proceed;
    }
}
